Skip to content

fix(worktree-reaper): fail closed when /proc cannot be scanned (#5740) - #6733

Closed
dezren39 wants to merge 1 commit into
mainfrom
queue/fallback-5740-proc-fail-closed
Closed

dezren39 wants to merge 1 commit into
mainfrom
queue/fallback-5740-proc-fail-closed

Conversation

@dezren39

@dezren39 dezren39 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Closes #5740.

Problem

proc_cwds() returned an empty list when os.listdir("/proc") failed, so a process-scan failure was indistinguishable from a successful scan with no matching processes. Both occupancy consumers failed open: evaluate_tree recorded the tree as a removal candidate, and reap_tree's fresh pre-removal re-check proceeded to git worktree remove / branch deletion. For a destructive tool, "could not determine" must not mean "safe to delete."

Fix (infra/worktree-reaper/bin/worktree_reaper.py)

  • proc_cwds() returns None when /proc cannot be listed — scan failure is now represented distinctly from a successful empty scan. Per-process readlink failures still skip just that process (the scan itself succeeded).
  • tree_occupied() fails closed on a None result (unknown occupancy counts as occupied); callers may pass a scan result or omit it for a fresh scan.
  • evaluate_tree keeps the candidate with reason occupied-unknown and gate record {"ok": false, "error": "proc-scan-failed"}.
  • reap_tree re-scans fresh before removal and keeps with reason occupied-unknown-at-removal — before any git worktree remove or branch deletion (mirrors the existing status-error-at-removal fail-closed pattern).
  • main() emits a stderr warning and a summary error naming the fail-closed retention when the run-wide scan fails.

Existing behavior is preserved for a successful empty scan and for a confirmed matching process.

Tests (infra/worktree-reaper/tests/test_worktree_reaper.py)

Discovered by the documented command (python3 tests/test_worktree_reaper.py, per the README):

  • Unit: a failed os.listdir("/proc") yields None (oracle: the listing is the failure point); unknown occupancy counts as occupied while an empty scan and a confirmed match keep their exact prior meaning.
  • Gate: failure injected at evaluation → candidate kept as occupied-unknown; failure injected only at the removal re-scan (first scan succeeds) → candidate kept as occupied-unknown-at-removal. Both assert on disk that the tree and its branch survive, i.e. neither git worktree remove nor branch deletion ran.

Run on the laptop: 12 passed, 0 failed (8 pre-existing + 4 new).

Note on the last acceptance criterion: the tests live in the documented test file and run under its documented command. Wiring this suite into a CI workflow is sibling issue #5946 (PR #6357), not this fix.

🤖 Queue worker lane code-q, fleet queue fallback mode (backend down) — issue #5740 is the task of record.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

proc_cwds returned an empty list when os.listdir("/proc") failed, so a scan failure was indistinguishable from a successful scan with no matching processes — and both occupancy consumers (evaluate_tree and reap_tree's pre-removal re-scan) treated it as unoccupied, passing the destructive removal gate on an inspection error.

Represent scan failure distinctly: proc_cwds returns None, tree_occupied fails closed on None, evaluate_tree keeps the candidate as occupied-unknown, reap_tree keeps as occupied-unknown-at-removal before any git worktree remove / branch -d, and main logs a warning naming the fail-closed retention. Successful empty scans and confirmed matches behave exactly as before.

Tests: failure injected at evaluation and at the removal re-scan (candidate tree + branch asserted retained on disk), plus unit tests pinning the None/empty/match contract. python3 tests/test_worktree_reaper.py: 12 passed.
@dezren39
dezren39 requested a review from a team as a code owner November 7, 2026 03:33
@coderabbitai

coderabbitai Bot commented Nov 7, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 5 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2f7a457a-55da-4867-8e90-700b9f9e69a6
Commits

Reviewing files that changed from the base of the PR and between da971f0 and f16f9c7.

Files selected for processing: 2

  • infra/worktree-reaper/bin/worktree_reaper.py
  • infra/worktree-reaper/tests/test_worktree_reaper.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T03:35:28.013227Z f16f9c7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@dezren39

dezren39 commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Closing as a duplicate of #6731 (merged 2026-11-07T03:34:00Z, merge commit 5df7c86), which fixes the same issue #5740.

Lanes code-z and code-q claimed #5740 in fallback mode 24 seconds apart and implemented in parallel (the deployed fleet binary has no fallback claim verb to refuse the second claim). #6731 landed first and is canonical; per the duplicate-PR protocol this PR was diffed against it before closing — see the salvage note on #6731. The only unique material here was end-to-end injection test coverage, recorded there; the implementations satisfy the same contract and no code needs porting. Issue #5740 is closed by #6731.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

worktree-reaper: fail closed when /proc cannot be scanned before removal

1 participant